package com.yunglu.pa03;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class PA03Activity extends Activity {
	private NetworkTask ntk = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pa03layout);
		final EditText server_name = (EditText) findViewById(R.id.text_name);
		final EditText port_number = (EditText) findViewById(R.id.text_port);
		final Button button_connect = (Button) findViewById(R.id.button_connect);
		final Button button_stop = (Button) findViewById(R.id.button_stop);
		final Button button_exit = (Button) findViewById(R.id.button_exit);

		button_connect.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ntk = new NetworkTask((DelayView) findViewById(R.id.view_delay));
				ntk.execute(server_name.getText().toString(), port_number
						.getText().toString());
				DelayView dv = (DelayView) findViewById(R.id.view_delay);
				dv.showData(true);

			}
		});
		button_stop.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (ntk != null) {
					// need to check because a user may click Stop without
					// clicking Connect
					ntk.onCancelled();
				}
				DelayView dv = (DelayView) findViewById(R.id.view_delay);
				dv.showData(false);
			}
		});
		button_exit.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (ntk != null) {
					ntk.onCancelled();
				}
				DelayView dv = (DelayView) findViewById(R.id.view_delay);
				dv.showData(false);
				finish();

			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.pa03layout, menu);
		return true;
	}

}
